#include"SeqList.h"
void SLInit(SL* ps)
{
    assert(ps);
    ps->a = NULL;
    ps->capacity = ps->size = 0;
}
void SLprint(SL* ps)
{
    assert(ps);
    int i = 0;
    for(i = 0; i < ps->size; i++)
    {
        printf("%d ",ps->a[i]);
    }
    printf("\n");
}
void CheckCapacity(SL* ps)
{
    if(ps->capacity == ps->size)
    {
        int newcpapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        SLDateType* tmp = (SLDateType*)realloc(ps->a, newcpapacity * sizeof(SLDateType));
        if(tmp == 0)
        {
            printf("realloc fail\n");
            exit(-1);
        }
        ps->a = tmp;
        ps->capacity = newcpapacity;
    }
}
void SLPushBach(SL* ps, SLDateType x)
{
    assert(ps);
    CheckCapacity(ps);//检查容量 放入数据
    ps->a[ps->size] = x;
    ps->size++;
}
void SLPushFront(SL* ps, SLDateType x)
{
    assert(ps);
    CheckCapacity(ps);//检查容量
    //挪动数据
    int end = ps->size - 1;
    while(end >= 0)
    {
        ps->a[end + 1] = ps->a[end];
        end--;
    }
    ps->a[0] = x;
    ps->size++;
}
void SLPopBach(SL* ps)
{
    assert(ps);
    assert(ps->size);
    ps->size--;
}
void SLPopFront(SL* ps)
{
    assert(ps);
    assert(ps->size);
    //挪动覆盖数据
    int begin = 1;
    while(begin < ps->size)
    {
        ps->a[begin - 1] = ps->a[begin];
        begin++;
    }
    ps->size--;
}
void SLInsert(SL* ps, int pos, SLDateType x)
{
    assert(ps);
    assert(pos >= 0 && pos <= ps->size);
    CheckCapacity(ps);
    int end = ps->size - 1;
    while(end >= pos)
    {
        ps->a[end + 1] = ps->a[end];
        end--;
    }
    ps->a[pos] = x;
    ps->size++;
}
void SLErase(SL* ps, int pos)
{   
    assert(ps);
    assert(pos >= 0 && pos <= ps->size);
    int begin = pos + 1;
    while(begin < ps->size)
    {
        ps->a[begin - 1] = ps->a[begin];
        begin++;
    }
    ps->size--;
}
int SLFind(SL* ps, SLDateType x)
{
     assert(ps);
     int i = 0;
     for(i = 0; i < ps->size; i++)
     {
        if(ps->a[i] == x)
        {
            return i;
        }
     }
     return -1;
}
void SLModify(SL* ps, int pos, SLDateType x)
{
    assert(ps);
    ps->a[pos] = x;
}
void SLDestory(SL* ps)
{
    if(ps != NULL)
    {
        free(ps->a);
        ps->a = NULL;
        ps->capacity = ps->size = 0;
    }
}